package com.chhuang.uresource.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.chhuang.core.bean.QueryBean;
import com.chhuang.core.validation.ValidGroupSave;
import com.chhuang.uresource.bean.ULabelQueryBean;
import com.chhuang.uresource.conts.SysConstants;
import com.chhuang.uresource.po.ULabel;
import com.chhuang.uresource.service.IULabelService;
import com.chhuang.utils.Assert;

@Controller
@RequestMapping("system/label")
public class SysULabelController {
	private static final Log log = LogFactory.getLog(SysULabelController.class);

	@Autowired
	private IULabelService labelService;
	
	/**
	 * 添加标签
	 * @param label
	 * @param bindingResult
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/addLabel", method=RequestMethod.PUT)
	public Object addLabel(@RequestBody @Validated(value={ValidGroupSave.class}) ULabel label, 
			BindingResult bindingResult) throws Exception {
		JSONObject json = new JSONObject();
		
		if(!bindingResult.hasErrors()){
			label.setCreateTime(new Date());
			labelService.save(label);
			json.put(SysConstants.Status.SUCCESS, true);
		}else {
			json.put(SysConstants.Status.SUCCESS, false);
			json.put(SysConstants.Status.RESULT, bindingResult.getAllErrors().get(0).getDefaultMessage());
		}
		
		log.debug(json);
		return json;
	}

	/**
	 * 删除标签
	 * @param idstr
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/delLabel/{idstr}", method=RequestMethod.DELETE)
	public Object delLabel(@PathVariable("idstr") String idstr) throws Exception {
		JSONObject json = new JSONObject();
		
		if(Assert.isValidString(idstr)) {
			String[] ids = idstr.split(SysConstants.Symbol.SEMICONLON);
			labelService.deleteByPrimaryKeys(ids);
			json.put(SysConstants.Status.SUCCESS, true);
		}else {
			json.put(SysConstants.Status.SUCCESS, false);
    		json.put(SysConstants.Status.RESULT, "参数有误");
		}

		log.debug(json);
		return json;
	}
	
	/**
	 * 查找所有标签
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value="/getAll",method=RequestMethod.GET)
	public Object getAll() throws Exception {
		JSONObject json = new JSONObject();
		
		List<ULabel> labels = labelService.findAll();
		
		json.put(SysConstants.Status.SUCCESS, true);
		json.put(SysConstants.Status.RESULT, labels);

		log.debug(json);
		return json;
	}
	
	/**
	 * 分页查找标签
	 * @param search
	 * @param limit
	 * @param offset
	 * @param sort
	 * @param isDesc
	 * @param isOr
	 * @param draw
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/getList",method=RequestMethod.GET)
	public Object getList(String search, Integer limit, Integer offset, 
			String sort, Boolean isDesc, Boolean isOr, Integer draw) throws Exception{
		JSONObject json = new JSONObject();
		
		ULabelQueryBean bean = new ULabelQueryBean();
		bean.setLabel(search);
		
		bean.setLimit(limit==null?10:limit);
		bean.setOffset(offset==null?0:offset);
		bean.setOr(isOr==null?true:isOr);
		bean.setSort(sort==null?"createTime":sort);
		bean.setDesc(isDesc==null?true:isDesc);
		
		QueryBean<ULabel> queryBean = labelService.seniorQuery(bean);
		
		json.put(SysConstants.Status.SUCCESS, true);
		json.put(SysConstants.Status.RESULT, queryBean);
		json.put(SysConstants.Status.OTHER, draw);
		
		log.debug(json);
		return json;
	}
	
}
